package net.caixiaomi.info.ui.discovery;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseFragment;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.interfaces.ComonFragmentImpl;
import net.caixiaomi.info.model.DlRecentRecordEntity;
import net.caixiaomi.info.model.MatchInfoForTeam;
import net.caixiaomi.info.model.MatchTeamInfo;
import net.caixiaomi.info.model.RecentRecordInfo;
import net.caixiaomi.info.model.TeamDetailFutureMatch;
import net.caixiaomi.info.model.TeamMatchInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by mac on 2018/9/11.
 */

public class TeamPersonRecordFragment extends BaseFragment implements ComonFragmentImpl {

    @BindView(android.R.id.list)
    RecyclerView mListView;
    @BindView(R.id.no_data_tv)
    TextView mNoData;

    private LeagueTeamAdapter mAdapter;

    private DlRecentRecordEntity entity;

    private String mTeamName;
    View headerView;

    @Override
    public void setType(int type) {

    }

    @Override
    public void setFilter(String filter) {

    }

    @Override
    protected int fragmentLayout() {
        return R.layout.league_shooter_layout;
    }

    @Override
    protected void onFragmentCreated(View view, @Nullable Bundle savedInstanceState) {
        mListView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new LeagueTeamAdapter(R.layout.item_match_detail_analysis);
        mListView.setAdapter(mAdapter);
        headerView=View.inflate(getActivity(),R.layout.header_match_detail_analysis,null);
        mAdapter.addHeaderView(headerView);

        mNoData.setVisibility(View.VISIBLE);
        headerView.setVisibility(View.GONE);
    }

    public void initData(DlRecentRecordEntity entity) {
        this.entity = entity;
        List<RecentRecordInfo> matchInfoFutureList=entity.getRecentRecordList();
        if(matchInfoFutureList.size()==0){
            mNoData.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.GONE);
            return;
        }
        mNoData.setVisibility(View.GONE);
        headerView.setVisibility(View.VISIBLE);
        List<MultiItemEntity>data=new ArrayList<>();
        data.addAll(matchInfoFutureList);
        mAdapter.setNewData(data);
        mTeamName=entity.getHomeTeam();
        initHeader(entity);
    }

    private void initHeader(DlRecentRecordEntity model){
        TextView text=headerView.findViewById(R.id.text);
//        helper.setGone(R.id.text, !TextUtils.isEmpty(model.getTeamAbbr()));
//        helper.setGone(R.id.action_divider, !TextUtils.isEmpty(model.getTeamAbbr()));
//        helper.setGone(R.id.line, !TextUtils.isEmpty(model.getTeamAbbr()));
//        helper.setGone(R.id.divider, TextUtils.isEmpty(model.getTeamAbbr()));
        if (!TextUtils.isEmpty(model.getHomeTeam())) {
            StringBuilder stringBuilder = new StringBuilder();
            String matchNum = CommonApp.getInstance().getString(R.string.C_MATCH_NUM, model.getMatchCount());

            stringBuilder.setLength(0);
            String hostWin = stringBuilder.append(model.getWin()).append(CommonApp.getInstance().getString(R.string.C_WIN)).toString();
            stringBuilder.setLength(0);
            String hostDraw = stringBuilder.append(model.getFlat()).append(CommonApp.getInstance().getString(R.string.C_DRAW)).toString();
            stringBuilder.setLength(0);
            String hostDefeat = stringBuilder.append(model.getNegative()).append(CommonApp.getInstance().getString(R.string.C_DEFEAT)).toString();
            stringBuilder.setLength(0);

            stringBuilder.append(CommonApp.getInstance().getString(R.string.C_RECENT_BATTLE)).append("  ").append(matchNum).append("  ").append(model.getHomeTeam()).append("  ").append(hostWin).append("  ").append(hostDraw).append("  ").append(hostDefeat);
            SpannableString spannableString = new SpannableString(stringBuilder);
            int start = stringBuilder.indexOf(matchNum);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.third_text)), start, start + matchNum.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            start = stringBuilder.indexOf(hostWin);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), start, start + hostWin.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            start = stringBuilder.indexOf(hostDraw);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.blue)), start, start + hostDraw.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            start = stringBuilder.indexOf(hostDefeat);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.green)), start, start + hostDefeat.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            text.setText(spannableString);
        }
    }

    public class LeagueTeamAdapter extends BaseQuickAdapter<MultiItemEntity, BaseViewHolder> {

        public LeagueTeamAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(BaseViewHolder helper, MultiItemEntity item) {
            try {
                int adapterPosition = helper.getAdapterPosition();
                RecentRecordInfo model = (RecentRecordInfo) item;

                helper.setText(R.id.league, model.getMatch());
                helper.setText(R.id.time, model.getDate());
                helper.setText(R.id.host, model.gethTeam());
                helper.setText(R.id.score, model.getScore());
                helper.setText(R.id.guest, model.getvTeam());
                helper.setText(R.id.result, model.getStatus());
                int textColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.third_text);
                int hostColor = textColor;
                int guestColor = textColor;
                if (TextUtils.equals(model.getStatus(), CommonApp.getInstance().getResources().getString(R.string.C_WIN))) {
                    textColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary);
                } else if (TextUtils.equals(model.getStatus(), CommonApp.getInstance().getResources().getString(R.string.C_DRAW))) {
                    textColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.blue);
                } else if (TextUtils.equals(model.getStatus(), CommonApp.getInstance().getResources().getString(R.string.C_DEFEAT))) {
                    textColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.green);
                }
                helper.setTextColor(R.id.result, textColor);
                helper.setTextColor(R.id.score, textColor);

                if(TextUtils.equals(model.gethTeam(), mTeamName)){
                    hostColor = textColor;
                }else{
                    guestColor = textColor;
                }

                helper.setTextColor(R.id.host, hostColor);
                helper.setTextColor(R.id.guest, guestColor);

                if (adapterPosition + 1 <= getData().size() - 1) {
                    MultiItemEntity next = getData().get(adapterPosition + 1);
                    helper.setGone(R.id.line, model.getItemType() == next.getItemType());
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
